/**
 * 
 */

$(function() {
	
	$.post("getUserInfo.do",{},function(data){
		getUserInfo(data);
	})
	
	$("#edit_phoneNum").click(editPhoneNum);
	$("#edit_email").click(editEmail);
	$("#phoneNum_save").click(phoneNumSave);
	$("#phoneNum_cancel").click(phoneNumCancel);
	$("#email_save").click(emailSave);
	$("#email_cancel").click(emailCancel);
	$("#changeTouxiang").click(changeTouxiang);
	$("#selectFile").change(uploadFile);
})

function editPhoneNum(){
	$(".phone_info_old").hide();
	$("#phone_info>.edit_info").show();
	
}
function editEmail(){
	$(".email_info_old").hide();
	$("#email_info>.edit_info").show();
	
}

function phoneNumCancel(){
	$("#phone_info>.edit_info").hide();
	$(".phone_info_old").show();
}
function phoneNumSave(){
	$("#phone_info>.edit_info").hide();
	$(".phone_info_old").show();
}
function emailCancel(){
	$("#email_info>.edit_info").hide();
	$(".email_info_old").show();
}
function emailSave(){
	$("#email_info>.edit_info").hide();
	$(".email_info_old").show();
}

function changeTouxiang(){
	$("#selectFile").click();
}


function uploadFile(){
	var acceptFileTypes=["image/jpeg","image/gif","image/png"];
	var file=$("#selectFile")[0].files[0];
	var fileType=file.type;
	console.log(file.type);
	if(file.size>512000){
		alert("图片不能大于500KB!");
		return;
	}
	var fileTypeFlag=false;
	for(var i=0;i<acceptFileTypes.length;i++){
		if(fileType==acceptFileTypes[i]){
			fileTypeFlag=true;
			break;
		}
	}
	if(!fileTypeFlag){
		alert("不支持的文件类型!");
		return;
	}
	var formData=new FormData();
	formData.append("img",file);
	$.ajax({
		url:"uploadTouxiang.do",
		processData: false,
		contentType: false,
		data:formData,
		type:"POST",
		success:function(data){
			var result =JSON.parse(data);
			console.log(result);
			var random=Math.random();
			var index=result.path.indexOf("WebContent\\");
			var path=result.path.substr(index+11);
			if(result.status=="success"){
				$("#touxiang img").attr("src",path);
			}
			
		},
		error:function(){
			alert("上传出错!");
		}
	})
}


function getUserInfo(data){
	var result=JSON.parse(data);
	if(result.status=="success"){
		$("#name").text(result.name);
		$("#employeeNum").text(result.employeeNum);
		$("#department").text(result.department);
		$("#phoneNUm").text(result.phoneNUm);
		$("#email").text(result.email);
		console.log(result.name);
		console.log(result.employeeNum);
	}
}
